﻿using System;
using System.Windows.Media;
using Might.Infrastructure.Annotations.Graphics;
using Might.Infrastructure.ModelicaDOM;

namespace Might.Modelica.Annotations.Graphics {
    ///<summary>
    ///</summary>
    public abstract class GraphicsFilledShapeEx : GraphicsItemEx, IGraphicsFilledShape {
        ///<summary>
        ///</summary>
        protected GraphicsFilledShapeEx() {
            lineColor = fillColor = new GraphicsColor(Colors.Black);
            pattern = LinePattern.Solid;
            fillPattern = FillPattern.None;
            Thickness = 0.25;
        }

        #region IGraphicsFilledShape Members

        public Brush Fill {
            get { return fill; }
            set {
                if (fill != value) {
                    fill = value;
                    OnChanged(this, "Fill");
                }
            }
        }
        private Brush fill;

        public Brush Stroke {
            get { return stroke; }
            set {
                if (stroke != value) {
                    stroke = value;
                    OnChanged(this, "Stroke");
                }
            }
        }
        private Brush stroke;

        [Mapping("lineThickness")]
        public double Thickness {
            get { return thickness; }
            set {
                if (thickness != value) {
                    thickness = value;
                    OnChanged(this, "Thickness");
                }
            }
        }
        private double thickness;

        #endregion

        #region Specific Modelica members

        ///<summary>
        ///</summary>
        [Mapping("lineColor")]
        public GraphicsColor lineColor {
            get {
                SolidColorBrush sl = Stroke as SolidColorBrush;
                if (sl == null) {
                    throw new ArgumentNullException();
                }
                return new GraphicsColor(sl.Color);
            }
            set { Stroke = new SolidColorBrush(value.color); }
        }

        ///<summary>
        ///</summary>        
        [Mapping("fillColor")]
        public GraphicsColor fillColor {
            get {
                SolidColorBrush sl = Fill as SolidColorBrush;
                if (sl == null) {
                    throw new ArgumentNullException();
                }
                return new GraphicsColor(sl.Color);
            }
            set { Fill = new SolidColorBrush(value.color); }
        }

        // TODO not supported yet
        ///<summary>
        ///</summary>
        [Mapping("pattern")]
        public LinePattern pattern { get; set; }

        // TODO not supported yet
        ///<summary>
        ///</summary>
        [Mapping("fillPattern")]
        public FillPattern fillPattern { get; set; }
        
        public override bool IsDefault {
            get {
                return base.IsDefault &&
                       lineColor.IsDefault &&
                       fillColor.IsDefault &&
                       pattern == LinePattern.Solid &&
                       fillPattern == FillPattern.None &&
                       Thickness == 0.25;
            }
        }

        #endregion
    }
}